package org.pine.dictionary.service;

import cn.hutool.core.bean.BeanUtil;
import org.pine.dictionary.bean.PageRequest;
import org.pine.dictionary.bean.excel.DictionaryDetailImportDto;
import org.pine.dictionary.bean.request.DictionaryDetailQueryRequest;
import org.pine.dictionary.bean.request.DictionaryDetailSaveRequest;
import org.pine.dictionary.bean.response.DictionaryDetailQueryResponse;
import org.pine.dictionary.service.bo.DictionaryDetailBo;
import org.pine.dictionary.service.query.DictionaryDetailQuery;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典明细表 服务类
 * </p>
 *
 * @author pine
 * @since 2024-05-15
 */
public interface IDictionaryDetailService extends BaseService<String, DictionaryDetailBo, DictionaryDetailQuery, DictionaryDetailSaveRequest, DictionaryDetailQueryRequest, DictionaryDetailQueryResponse> {

    @Override
    default DictionaryDetailBo transformToEntity(DictionaryDetailSaveRequest request) {
        DictionaryDetailBo dictionaryDetailBo = new DictionaryDetailBo();
        BeanUtil.copyProperties(request, dictionaryDetailBo);
        return dictionaryDetailBo;
    }

    @Override
    default DictionaryDetailQueryResponse transformToResponse(DictionaryDetailBo dictionaryDetail) {
        DictionaryDetailQueryResponse vo = new DictionaryDetailQueryResponse();
        BeanUtil.copyProperties(dictionaryDetail, vo);
        return vo;
    }

    @Override
    default PageRequest<DictionaryDetailQuery> transformToQuery(PageRequest<DictionaryDetailQueryRequest> request) {
        DictionaryDetailQuery query = new DictionaryDetailQuery();
        if (Objects.nonNull(request.getData())) {
            BeanUtil.copyProperties(request.getData(), query);
        }

        PageRequest<DictionaryDetailQuery> pageRequest = new PageRequest<>();
        pageRequest.setPageNum(request.getPageNum());
        pageRequest.setPageSize(request.getPageSize());
        pageRequest.setData(query);
        return pageRequest;
    }

    /**
     * 将上传的内容转换成实体类，转换之后的数据保存到数据库中
     *
     * @param importDtos 上传dto
     * @return 实体bo
     */
    default List<DictionaryDetailBo> transformToEntities(List<DictionaryDetailImportDto> importDtos) {
        List<DictionaryDetailBo> resultList = new ArrayList<>();
        for (DictionaryDetailImportDto importDto : importDtos) {
            DictionaryDetailBo entity = new DictionaryDetailBo();
            BeanUtils.copyProperties(importDto, entity);
            resultList.add(entity);
        }
        return resultList;
    }
}
